import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import os.path
from config import datas

class TestRepoManagement:
    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.09/repoManagement/GET_get_all_helm_repo.yaml'))
    def test_get_all_helm_repo(self, yaml):
        # 查询所有Helm仓库信息
        uri = yaml['request']['url']
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml',
                             YamlUtil().read_testcase_yaml('v24.09/repoManagement/GET_get_specified_helm_repo.yaml'))
    def test_get_specified_helm_repo(self, yaml):
        # 查询指定Helm仓库信息
        uri = yaml['request']['url'].replace("{repo}", datas.test_repo_management["repo"])
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.09/repoManagement/POST_add_helm_repo.yaml'))
    def test_add_helm_repo(self, yaml):
        # 添加Helm仓库
        uri = yaml['request']['url']
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        data["name"] = datas.test_repo_management["add_repo_name"]
        data["url"] = datas.test_repo_management["add_repo_url"]
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.09/repoManagement/PUT_modify_helm_repo.yaml'))
    def test_modify_helm_repo(self, yaml):
        # 修改Helm仓库
        uri = yaml['request']['url'].replace("{repo}", datas.test_repo_management["modify_repo_name"])
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        data["name"] = datas.test_repo_management["modify_repo_name"]
        data["url"] = datas.test_repo_management["modify_repo_url"]
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml',
                             YamlUtil().read_testcase_yaml('v24.09/repoManagement/DELETE_delete_helm_repo.yaml'))
    def test_delete_helm_repo(self, yaml):
        # 删除Helm仓库
        uri = yaml['request']['url'].replace("{repo}", datas.test_repo_management["delete_repo_name"])
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml',
                             YamlUtil().read_testcase_yaml('v24.09/repoManagement/POST_sync_all_helm_repo.yaml'))
    def test_sync_all_helm_repo(self, yaml):
        # 同步全部Helm仓库
        uri = yaml['request']['url']
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml',
                             YamlUtil().read_testcase_yaml('v24.09/repoManagement/POST_sync_specified_helm_repo.yaml'))
    def test_sync_specified_helm_repo(self, yaml):
        # 同步指定Helm仓库
        uri = yaml['request']['url'].replace("{repo}", datas.test_repo_management["sync_repo_name"])
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml',
                             YamlUtil().read_testcase_yaml('v24.09/repoManagement/GET_get_helm_repo_sync_status.yaml'))
    def test_get_helm_repo_sync_status(self, yaml):
        # 获取仓库同步状态
        uri = yaml['request']['url'].replace("{repo}", datas.test_repo_management["sync_repo_name"])
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.09/repoManagement/GET_get_charts.yaml'))
    def test_get_charts(self, yaml):
        # 查询仓库中的Chart列表（最新）
        uri = yaml['request']['url']
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml',
                             YamlUtil().read_testcase_yaml('v24.09/repoManagement/POST_upload_chart_to_localrepo.yaml'))
    def test_upload_chart_to_locorepo(self, yaml):
        # 上传Helm Chart
        uri = yaml['request']['url']
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        # file_path 第二种写法。每次os.path.dirname()向上跳一级目录
        # file_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) +
        # '/data/v24.09/repoManagement/stress-runner-ext-long.tgz'
        # 文件可以重复上传，所以不用检查文件是否已存在
        file_path1 = os.path.dirname(
            __file__) + '/../../../data/v24.09/repoManagement/test-chart-stress-runner-ext-v1.tgz'
        with open(file_path1, 'rb') as file1:
            req_file1 = RequestUtil().send_request(method, url, files={'chart': file1})
            assert req_file1.status_code == yaml['validate'][0]['equals']['status_code']
        # 继续上传2个版本，后续删除使用
        file_path2 = os.path.dirname(
            __file__) + '/../../../data/v24.09/repoManagement/test-chart-stress-runner-ext-v2.tgz'
        with open(file_path2, 'rb') as file2:
            req_file2 = RequestUtil().send_request(method, url, files={'chart': file2})
            assert req_file2.status_code == yaml['validate'][0]['equals']['status_code']
        file_path3 = os.path.dirname(
            __file__) + '/../../../data/v24.09/repoManagement/test-chart-stress-runner-ext-v3.tgz'
        with open(file_path3, 'rb') as file3:
            req_file3 = RequestUtil().send_request(method, url, files={'chart': file3})
            assert req_file3.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml(
        'v24.09/repoManagement/DELETE_delete_chart_specified_version.yaml'))
    def test_delete_chart_specified_version(self, yaml):
        # 删除Helm Chart（指定版本）
        uri = yaml['request']['url'].replace("{chart}", datas.test_repo_management["delete_chart_name"])
        uri = uri.replace("{version}", datas.test_repo_management["delete_chart_specified_version"])
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml(
        'v24.09/repoManagement/DELETE_delete_chart_all_version.yaml'))
    def test_delete_chart_all_version(self, yaml):
        # 删除Helm Chart（全部版本）
        # 前面测试用例共上传3个版本，上一条用例删除1个指定版本，本条用例删除剩余2个版本
        uri = yaml['request']['url'].replace("{chart}", datas.test_repo_management["delete_chart_name"])
        url = datas.baseUrl + uri
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']
